﻿Imports System.Net
Imports System.IO

Public Class Way2SMS
    Implements ISmsSender

    Private Function GetRequest(ByVal url As [String], ByVal nameValueCollection As NameValueCollection) As HttpWebRequest
        ' Here we convert the nameValueCollection to POST data.
        ' This will only work if nameValueCollection contains some items.
        Dim parameters = New StringBuilder()

        For Each key In nameValueCollection
            parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nameValueCollection(key)))
        Next

        parameters.Length -= 1

        ' Here we create the request and write the POST data to it.
        Dim request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
        request.Proxy = New WebProxy("184.107.41.74")
        request.ContentType = "application/x-www-form-urlencoded"
        request.Method = "POST"
        request.AutomaticDecompression = System.Net.DecompressionMethods.GZip
        'request.Proxy = IGNOU.IGNOURobot.prxy
        'Accept-Encoding: gzip,deflate
        Using writer = New StreamWriter(request.GetRequestStream())
            writer.Write(parameters.ToString())
        End Using

        Return request
    End Function

    Public Function DoLogin(ByVal userName As String, ByVal password As String) As BOResult(Of String) Implements ISmsSender.DoLogin
        Dim obj As New BOResult(Of String)
        Dim wc As New System.Net.WebClient
        Dim d As New Specialized.NameValueCollection

        'username=9811810148&password=1346&button=Login
        d.Add("username", userName)
        d.Add("password", password)
        d.Add("button", "Login")

        'GetRequest("http://184.107.41.74/Login1.action", d) 
        Dim requeest As Net.HttpWebRequest = GetRequest("http://site1.way2sms.com/Login1.action", d) 'Net.WebRequest.Create("http://www.contoso.com")

        ' requeest.Proxy = New WebProxy("184.107.41.74")

        requeest.AllowAutoRedirect = False
        requeest.Method = "POST"

        Dim resp As Net.HttpWebResponse = requeest.GetResponse()

        If resp.StatusCode = 302 AndAlso resp.GetResponseHeader("Location").Contains("Main.action") Then

            Dim cookies = resp.GetResponseHeader("Set-Cookie")

            Dim match = Regex.Match(cookies, "JSESSIONID=[^;]*")
            If match.Success Then
                'Dim mywc As New WebClient
                'mywc.Headers.Add("Cookie", cookies)
                'mywc.Headers.Add("Accept-Encoding", "gzip,deflate")
                'mywc.DownloadString(New System.Uri(resp.GetResponseHeader("Location")))
                obj.OperationStatus = True
                obj.Result = match.Value
            End If

        Else
            obj.OperationStatus = False
            obj.Status = "Invalid Username or Password"
        End If

        ' Dim response = wc.UploadValues("http://site1.way2sms.com/Login1.action", d)



        'Dim mem2 As New IO.MemoryStream(response)

        'Dim reader As New System.IO.StreamReader(mem2)
        'Dim t = reader.ReadToEnd

        'If t.Contains("http://180.150.134.195/home.php?Login=1") Then
        '    Dim cookies = wc.ResponseHeaders.Get("Set-Cookie")

        '    Dim match = Regex.Match(cookies, "JSESSIONID=[^;]*")
        '    If match.Success Then
        '        obj.OperationStatus = True
        '        obj.Result = match.Value
        '    End If

        'Else
        '    obj.OperationStatus = False
        '    obj.Status = "Invalid Username or Password"
        'End If

        'obj.Result = t
        Return obj
    End Function

    Public Function SendSMS(ByVal userName As String, ByVal password As String, ByVal msg As String, ByVal mobileNo As String) As BOResult(Of String) Implements ISmsSender.SendSMS
        Dim obj As New BOResult(Of String)
        Dim o = Me.DoLogin(userName, password)
        If o.OperationStatus Then
            Dim wc As New System.Net.WebClient
            wc.Headers.Add("Cookie", o.Result)
            Dim d As New Specialized.NameValueCollection
            'HiddenAction=instantsms&Action=sa65sdf656fdfd&MobNo=9899888450&textArea=hi%2Cmanish.
            d.Add("HiddenAction", "instantsms")
            d.Add("Action", "sa65sdf656fdfd")
            d.Add("MobNo", mobileNo)
            d.Add("textArea", msg)


            Dim requeest As Net.HttpWebRequest = GetRequest("http://site1.way2sms.com/quicksms.action", d) 'Net.WebRequest.Create("http://www.contoso.com")

            requeest.AllowAutoRedirect = False
            requeest.Method = "POST"
            requeest.Headers.Add("Cookie", o.Result)
            Dim resp As Net.HttpWebResponse = requeest.GetResponse()

            Dim st = resp.GetResponseStream

            Dim reader As New System.IO.StreamReader(st)
            Dim t = reader.ReadToEnd

            'Dim response = wc.UploadValues("http://site1.way2sms.com/quicksms.action", d)

            'Dim mem2 As New IO.MemoryStream(response)

            'Dim reader As New System.IO.StreamReader(mem2)
            'Dim t = reader.ReadToEnd

            If t.Contains("Message has been submitted successfully") Then
                obj.Status = "SMS Sent Successfully"
                obj.OperationStatus = True
            Else
                obj.OperationStatus = False
                obj.Status = "Error Occurred while Sending"
            End If
            Return obj
        Else
            Return o
        End If
    End Function
End Class
